#include "ring.h"

void ring(){

  int32_t node_pids[NUM_PROCS];
  int32_t parent_pid = obtenir_pid();
  int32_t args[4];
  int i;

  /* we give the parent id to the nodes, so they can
   * get the ids of the other nodes from it via message */
  args[0] = parent_pid;
  /* fork the NUM_PROCS children */
  for(i=0; i < NUM_PROCS; i++){ 
    args[1] = i;
    node_pids[i] = fourchette(32, "ring_node", args);
    ecrire("created process: ");
    char s[4];
    ecrire(itoa((int)(node_pids[i]), s, 4));
    ecrire("\n");
  }

  /* set up the nodes with the id of the next node */
  for(i=0; i < NUM_PROCS; i++) {
    envoyer_entier(node_pids[i], node_pids[(i+1) % NUM_PROCS], 9);
    ecrire("Send next_node_pid to process: ");
    char s[4];
    ecrire(itoa((int)(node_pids[i]), s, 4));
    ecrire("\n");
  }

  /* make the control node start */
  envoyer_chaine(node_pids[NUM_PROCS-1], "Initial message", 9);

  /* die and let the nodes do their thing */
  ecrire("Parent will terminate now\n");
  quitter(0);
}
